package com.xj.algorithm.leetcode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 键盘行
 */
public class L500_判断单词是否是有键盘上同一行的字符组成的 {

    /**
     * 给定一个单词列表，只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。
     * 示例：
     * <p>
     * 输入: ["Hello", "Alaska", "Dad", "Peace"]
     * 输出: ["Alaska", "Dad"]
     *  
     * <p>
     * 注意：
     * <p>
     * 你可以重复使用键盘上同一字符。
     * 你可以假设输入的字符串将只包含字母。
     */
    public static void main(String[] args) {
        String[] words = new String[]{"Hello", "Alaska", "Dad", "Peace"};
        String[] res=findWords(words);
        for (String v:res){
            System.out.print(v+",");
        }
        System.out.println();

        String[] words2 = new String[]{};
        String[] res2=findWords(words2);
        for (String v:res2){
            System.out.print(v+",");
        }
        System.out.println();

        String[] words3 = new String[]{"Hello", "Alaska", "Dad", "Peace"};
        String[] res3=findWordsGood(words3);
        for (String v:res3){
            System.out.print(v+",");
        }
        System.out.println();
    }

    //自己的解法：hashmap并且包含操作。少不了的多重循环。忽略大小写
    public static String[] findWords(String[] words) {
        Map<String,String> map = new HashMap();
        map.put("qwertyuiopQWERTYUIOP", "qwertyuiopQWERTYUIOP");
        map.put("asdfghjklASDFGHJKL", "asdfghjklASDFGHJKL");
        map.put("zxcvbnmZXCVBNM", "zxcvbnmZXCVBNM");

        List<String> list=new ArrayList<>();
        for (String w : words) {

            //找到当前单词属于键盘的哪一行
            String mark=null;
            int len = w.length();
            boolean flag=true;
            for (int i=0;i<len;i++){
                String tmp= String.valueOf(w.charAt(i));

                if(mark==null) {
                    //遍历map
                    for (Map.Entry<String, String> entry : map.entrySet()) {
                        String key = entry.getKey();
                        if (key.contains(tmp)) {
                            mark = key;
                            break;
                        }
                    }
                }else{
                    if(!mark.contains(tmp)){
                        flag=false;
                        break;
                    }
                }
            }

            if(flag==true){
                list.add(w);
            }

        }

        return list.toArray(new String[0]);
    }


    //精彩解法：将每个字母的行号进行确定
    public static String[] findWordsGood(String[] words) {
        if(words==null||words.length==0) return new String[0];

        int[] map = {2,3,3,2,1,2,2,2,1,2,2,2,3,3,1,1,1,1,2,1,1,3,1,3,1,3};
        List<String> list = new ArrayList<>();
        for(String word:words){
            String tempWord = word.toUpperCase();
            int temp = map[tempWord.charAt(0)-65];
            boolean flag = true;

            for(int i=1;i<tempWord.length();i++){
                if(temp != map[tempWord.charAt(i)-65]){
                    flag = false;
                    break;
                }
            }
            if(flag) list.add(word);
        }
        return list.toArray(new String[list.size()]);
    }

}
